<template>
  <div class="container">
    <!--添加学院对话框表单-->
    <el-form :inline="true" :model="listQuery" class="demo-form-inline"  :rules="rules" >
      <el-form-item>
        <el-button type="primary" @click="addCollege">添加学院</el-button>
      </el-form-item>
    </el-form>
<!-- 添加表单-->
    <el-dialog title="添加学院" :visible.sync="dialogFormVisible" @close="onDialogClose">
      <el-form :model="listQuery" :rules="rules" ref="form">
        <el-form-item label="学院名称" :label-width="formLabelWidth" prop="collegeName">
          <el-input v-model="listQuery.collegeName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditCollege('form')">确 定</el-button>
      </div>
    </el-dialog>
    <!--学院列表表格-->
    <el-table
      :data="collegeData"
      border
      v-loading="loading"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"
      >
      </el-table-column>
      <el-table-column
        prop="collegeName"
        label="名称"
        >
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle @click="handleEditClick(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="handleDeleteClick(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.page"
        :page-sizes="[5, 10, 15]"
        :page-size="listQuery.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {deleteCollege,addCollege, getCollegeList} from "../../api/college";

  export default {
    name: "",
    data() {
      return {
        loading:false,
        collegeData: [],
        dialogFormVisible:false,
        formLabelWidth: '120px',
        total:0,
        listQuery: {
          page: 1,
          size: 10,
          typeForEdit:'',
          collegeName:'',
          id:''
        },
        rules: {
          collegeName: [
            {required: true, message: '请输入学院', trigger: 'blur'},
          ]
        }
      }
    },
    methods: {
      //关闭添加
      onDialogClose(){
        this.listQuery.collegeName=''
      },
      listCollege() {
        this.loading=true
        getCollegeList(this.listQuery).then(res => {
          this.collegeData = res.data.pageList
          this.total=res.data.total
          this.loading=false
        }).catch(err=>{
          this.loading=false
        })
      },
      //修改或添加学院
      addOrEditCollege(formName){
        this.loading=true
        this.$refs[formName].validate((valid)=>{
          if (valid) {
            this.dialogFormVisible = false
            addCollege(this.listQuery).then(res=>{
              this.$message.success("添加成功")
              this.listCollege()
            }).catch(err=>{
              this.loading=false
            })
          }else {
            this.$message.warning("请完善信息")
          }
        })

      },
      //添加学院
      addCollege(){
        this.dialogFormVisible=true
        this.listQuery.typeForEdit="add"

      },
      //修改学院信息
      handleEditClick(param){
        this.dialogFormVisible=true
        this.listQuery.id=param.id
        this.listQuery.collegeName=param.collegeName
        this.listQuery.TypeForEdit="edit"
      },
      //删除学院
      handleDeleteClick(param){
        this.loading=true
        this.listQuery.id=param.id
        deleteCollege(this.listQuery).then(res=>{
          this.$message.success("删除成功")
        }).catch(error=>{
          this.$message.error("删除失败")
          this.loading=false
        })
      },
      //分页相关
      handleSizeChange(val) {
        this.listQuery.size=val
        this.listCollege()
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.listQuery.page=val
        this.listCollege()
        console.log(`当前页: ${val}`);
      }
    },
    created() {
      this.listCollege()
    }
  }
</script>

<style scoped>
  .demo-form-inline{
    margin-top: 20px;
    margin-left: 10px;
  }
</style>
